package com.fishery.processing.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.entity.GlobalException;
import com.fishery.entity.Result;
import com.fishery.entity.StatusCode;
import com.fishery.processing.client.OrderClient;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Product;
import com.fishery.processing.entity.client.OrderVo;
import com.fishery.processing.entity.vo.CraftVo;
import com.fishery.processing.entity.vo.ProductVo;
import com.fishery.processing.service.CraftService;
import com.fishery.processing.service.ProductService;
import com.fishery.processing.util.ConvertUtil;
import com.fishery.processing.util.UpdateProxy;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Api(tags = "加工产品相关接口")
@CrossOrigin
@RestController
@RequestMapping("/processingFactory/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UpdateProxy updateProxy;

    @Autowired
    private ConvertUtil convertUtil;

    @Autowired
    private OrderClient orderClient;

    @PostMapping("/")
    @ApiOperation("新增加工产品")
    public Result createProduct(@Valid @RequestBody @ApiParam(required = true,value = "productVo") ProductVo productVo){
        return productService.createProduct(convertUtil.convert(productVo,Product.class)) == 1
                ? Result.success("新增成功") : Result.error("新增失败");
    }

    @PutMapping("/")
    @ApiOperation("修改产品信息")
    public Result updateProduct(@Valid @RequestBody @ApiParam(required = true,value = "productVo") ProductVo productVo){
        Product product = productService.searchProductById(productVo.getId());
        BeanUtils.copyProperties(productVo,product);
        //使用反射对修改操作进行检查
        return ((ProductService)(updateProxy.bind(productService).getProxyInstance())).updateProduct(product) == 1
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除加工产品")
    public Result deleteProduct(@PathVariable(value = "id") String productId){
        return productService.deleteProduct(productId) == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据产品id获取加工产品")
    public Result searchProductById(@PathVariable(value = "id") String productId){
        return Result.success()
                .data(convertUtil.convert(productService.searchProductById(productId),ProductVo.class));
    }

    @DeleteMapping("/{productId}/{sort}")
    @ApiOperation("删除加工产品指定步骤的工艺")
    public Result deleteCraft(@PathVariable(value = "productId") String productId
                                ,@PathVariable(value = "sort") Integer sort){
        return productService.deleteCraft(productId,sort) == 1
                ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("/{productId}/{craftId}/{sort}")
    @ApiOperation("添加加工品指定步骤的工艺")
    public Result addCraft(@PathVariable(value = "productId") String productId
                            ,@PathVariable(value = "craftId") String craftId
                            ,@PathVariable(value = "sort") Integer sort){
        return productService.addCraft(productId,craftId,sort) == 1
                ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping("/{productId}/{craftId}/{sort}")
    @ApiOperation("修改加工品指定步骤的工艺")
    public Result updateCraft(@PathVariable(value = "productId") String productId
                                ,@PathVariable(value = "craftId") String craftId
                                ,@PathVariable(value = "sort") Integer sort){
        return productService.updateCraft(productId,craftId,sort) == 1
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("/factoryProduct/{id}/{page}/{size}")
    @ApiOperation("分页获取加工厂的加工产品")
    public Result searchProductsOfFactory(@PathVariable(value = "id") String factoryId
                                            ,@PathVariable(value = "page") Integer page
                                            ,@PathVariable(value = "size") Integer size){
        return Result.success().
                data(convertUtil.convert(productService.searchProductOfFactory(factoryId, page, size),ProductVo.class));
    }

    @GetMapping("/factoryProduct/{id}")
    @ApiOperation("获取加工厂的加工产品")
    public Result searchProductsOfFactory(@PathVariable(value = "id") String factoryId){
        return Result.success()
                .data(convertUtil.convert(productService.searchProductOfFactory(factoryId),ProductVo.class));
    }

}

